package com.yc;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.yc.base.*;
import com.yc.util.DataSourceUtil;

import java.util.Collections;
import java.util.Scanner;

public class CodeGenerator {

    // 如果模板引擎是 freemarker
    private static final String templateMapperPath = "/templates/mapper.java";
    private static final String templateMapperXmlPath = "/templates/mapper.xml";
    private static final String templateEntityPath = "/templates/entity.java";
    private static final String templateControllerPath = "/templates/controller.java";
    private static final String templateServicePath = "/templates/service.java";
    private static final String templateServiceImplPath = "/templates/serviceImpl.java";

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入" + tip + "：");
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StrUtil.isNotEmpty(ipt)) {
                return ipt;
            }

        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        String moduleName = scanner("输入模块名字");
        String userDir = System.getProperty("user.dir");
        String outputDir = String.format("%s/%s/src/main/java", userDir, moduleName);
        GlobalConfig config = new GlobalConfig.Builder()
                .fileOverride()
                .outputDir(outputDir)
                .author("bao")
                .enableSwagger()
                .dateType(DateType.TIME_PACK)
                .commentDate("yyyy-MM-dd")
                .build();

        PackageConfig pkConfig = new PackageConfig.Builder()
                .parent(scanner("输入父级包名"))
                .entity("entity")
                .service("service")
                .serviceImpl("service.impl")
                .mapper("mapper")
                .xml("mapper")
                .controller("controller")
                .pathInfo(Collections.singletonMap(OutputFile.xml, String.format("%s/%s/src/main/resources/mapper", userDir, moduleName)))
                .build();

        TemplateConfig tConfig = new TemplateConfig.Builder()
                .disable(TemplateType.ENTITY)
                .entity(templateEntityPath)
                .service(templateServicePath)
                .serviceImpl(templateServiceImplPath)
                .mapper(templateMapperPath)
                .xml(templateMapperXmlPath)
                .controller(templateControllerPath)
                .build();
        StrategyConfig stConfig = new StrategyConfig.Builder()
                .enableSkipView()
                .disableSqlFilter()
                .addInclude(scanner("表名，多个英文逗号分割").split(","))
                .addTablePrefix(scanner("输入表前缀").split(","))
                .build()
                .entityBuilder()
                .superClass(BaseSimpleEntity.class)
                .disableSerialVersionUID()
                .enableChainModel()
                .enableLombok()
                .enableRemoveIsPrefix()
                .enableTableFieldAnnotation()
                .naming(NamingStrategy.underline_to_camel)
                .columnNaming(NamingStrategy.underline_to_camel)
                .addSuperEntityColumns("id", "create_by", "update_by", "create_time", "update_time", "deleted", "status", "sort")
                .idType(IdType.INPUT)
                .formatFileName("%s")
                .build()
                .controllerBuilder()
                .superClass(BaseController.class)
                .enableHyphenStyle()
                .enableRestStyle()
                .formatFileName("%sController")
                .build()
                .serviceBuilder()
                .superServiceClass(BaseService.class)
                .superServiceImplClass(BaseServiceImpl.class)
                .formatServiceFileName("I%sService")
                .formatServiceImplFileName("%sServiceImpl")
                .build()
                .mapperBuilder()
                .superClass(CommonMapper.class)
                .enableMapperAnnotation()
                .enableBaseResultMap()
                .enableBaseColumnList()
                .formatMapperFileName("%sMapper")
                .formatXmlFileName("%sMapper")
                .build();


        AutoGenerator ag = new AutoGenerator(DataSourceUtil.getDataSourceConfig());
        ag.global(config)
                .template(tConfig)
                .strategy(stConfig)
                .packageInfo(pkConfig);

        ag.execute(new FreemarkerTemplateEngine());
    }
}